package filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/*
* 为登录用户实现过滤器，只有通过login1才能实现页面跳转
*
* */
public class LoginFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpSession session = request.getSession ();
        Object user = session.getAttribute ("user");
        //获取servlet的路径请求信息
        String servletPath = request.getServletPath ();
        if (null == user && !servletPath.contains ("login1") ) {
            servletRequest.getRequestDispatcher ("login1.jsp").forward (servletRequest,servletResponse);
        } else {
            filterChain.doFilter (servletRequest,servletResponse);
        }
    }

    @Override
    public void destroy() {

    }
}
